package libs.yzx.zaza.za;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class BitmapCompress {

    public File compressImageToNew(File sourceFile , int maxKB , int maxWidth, int maxHeight , Bitmap.Config config , File target){
        Bitmap bmp = getBitmapWithTargetLength(maxWidth, maxHeight, config, sourceFile);
        if(bmp == null) return null;
        File result = compressImageToNew(bmp, maxKB, target);
        bmp.recycle();
        return result;
    }

    public File compressImageToNew(Bitmap source , int maxKB , File target){
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        final int maxByte = maxKB * 1024;
        int quality = 100;
        source.compress(Bitmap.CompressFormat.JPEG , quality , out);
        while(out.size() > maxByte){
            out.reset();
            int dis = out.size() / maxByte;
            if(dis <2)  quality -= 4;
            else if(dis < 4) quality -= 7;
            else quality -= 10;
            source.compress(Bitmap.CompressFormat.JPEG,quality,out);
        }
        BufferedOutputStream bos = null;
        try{
            bos = new BufferedOutputStream(new FileOutputStream(target));
            bos.write(out.toByteArray());
            bos.flush();
            bos.close();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            out.reset();
            if(bos != null)
                try{
                    bos.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
        }
        return target;
    }



    public Bitmap getBitmapWithTargetLength(int width , int height,Bitmap.Config config ,File file){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = getInSampleSize(width,height,file);
        options.inPreferredConfig = config;
        return BitmapFactory.decodeFile(file.getAbsolutePath() , options);
    }


    public int getBitmapSizeWithTargetLength(int width , int height , Bitmap.Config config , File file){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inSampleSize = getInSampleSize(width,height,file);
        BitmapFactory.decodeFile(file.getAbsolutePath(),options);
        return options.outHeight * options.outWidth * (config == Bitmap.Config.ARGB_8888 ? 4 : 2);
    }


    public int getInSampleSize(int targetWidth , int targetHeight , File file){
        int[] wh = getImageFileWidthHeight(file);
        int originWidth = wh[0];
        int originHeight = wh[1];
        if(originWidth < targetWidth && originHeight < targetHeight)
            return 1;
        int scaleW = (int)(originWidth / targetWidth + 0.5);
        int scaleH = (int)(originHeight / targetHeight + 0.5);
        return Math.max(scaleH,scaleW);
    }


    private int[] getImageFileWidthHeight(File file){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(),options);
        return new int[]{options.outWidth, options.outHeight};
    }


}
